package com.qzsoft.zhjd.machine.zhgcloud;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.qzsoft.zhjd.machine.zhgcloud.realtime.ZhgRealtimeHandler;
import com.qzsoft.zhjd.machine.zhgcloud.refuelrecords.ZhgRefuelRecordsHandler;
import com.qzsoft.zhjd.machine.zhgcloud.worktime.ZhgWorktimeHandler;


/**
 * @ClassName ZhgcloudTask
 * @Description 机械指挥官定时任务
 * @author linhuawei
 * @since 2021-10-11
 */
@Component
public class ZhgcloudTask {

    @Autowired
    private ZhgWorktimeHandler zhgWorktimeHandler;

    @Autowired
    private ZhgRealtimeHandler zhgRealtimeHandler;
    
    @Autowired
    private ZhgRefuelRecordsHandler zhgRefuelRecordsHandler;
    
    /**
     * @Title realtimeTask
     * @Description 每分钟获取实时数据
     */
    //@Scheduled(cron = "5 0/1 * * * ?")
    public void realtimeTask() {
        zhgRealtimeHandler.realtime();
    }

    /**
     * @Title refuelRecordsTask
     * @Description 每天凌晨5分获取昨天加油记录
     */
    //@Scheduled(cron = "0 5 0 * * ?")
    public void refuelRecordsTask() {
        // 当前日期作为结束时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String endDt = sdf.format(date);
        
        // 获取昨天日期作为开始时间
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);
        String startDt = sdf.format(cal.getTime());
        
        // 执行加油记录数据采集
        zhgRefuelRecordsHandler.refuelRecords(startDt, endDt);
    }
    
    /**
     * @Title worktimeTask
     * @Description 每个小时统计工时信息
     */
    //@Scheduled(cron = "0 5 0/1 * * ?")
    public void worktimeTask() {
        // 获取上个小时的日期
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, -1);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        zhgWorktimeHandler.worktime(cal.getTime());
    }
    
}
